<!-- 部门管理页面 -->
<template>
  <div>
    <!-- 根据参数查询 和 添加数据 -->
    <el-row>
      <el-col :span="6">
        <el-input
          v-model="searchForm.phonenumber"
          placeholder="请输入手机号码"></el-input
      ></el-col>
      <el-col :span="6">
        <el-select v-model="searchForm.state" placeholder="请选择用户状态">
          <el-option label="有效" value="0"></el-option>
          <el-option label="无效" value="1"></el-option>
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-select v-model="searchForm.sex" placeholder="请选择用户性别">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
        </el-select>
      </el-col>
      <el-col :span="6"
        ><el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd"
          >添加</el-button
        >
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-refresh"
          @click="refresh"
          >刷新</el-button
        ></el-col
      >
    </el-row>
    <!--    查询-->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column
        width="120"
        prop="mobile"
        label="手机"></el-table-column>
      <el-table-column
        width="210"
        prop="name"
        label="真实姓名"></el-table-column>
      <el-table-column
        width="210"
        prop="smallname"
        label="用户名"></el-table-column>
      <el-table-column
        width="210"
        :formatter="getStatus"
        prop="sex"
        label="性别"></el-table-column>
      <el-table-column
        width="210"
        prop="birthday"
        label="生日"></el-table-column>
      <el-table-column
        width="210"
        prop="email"
        label="邮箱"></el-table-column>
      <el-table-column width="210" prop="qq" label="qq号"></el-table-column>
      <el-table-column
        width="210"
        prop="zjno"
        label="证件号"></el-table-column>
      <el-table-column
        width="210"
        prop="state"
        :formatter="getStatus1"
        label="状态"></el-table-column>
      <el-table-column
        width="210"
        prop="cjtime"
        label="创建时间"></el-table-column>
      <el-table-column
        width="210"
        prop="gxtime"
        label="更新时间"></el-table-column>
      <!-- 操作设置 -->
      <el-table-column fixed="right" width="210" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑
          </el-button>
          <el-button type="danger" @click="deleteDept(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 6, 12, 24, 120]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--    添加修改弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="关于信息" :label-width="formLabelWidth">
          <el-input v-model="form.aboutId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="form.orderNum" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  //数据
  data() {
    return {
      imageUrls: "", //上传照片设置
      //table的表格数据
      tableData: [],
      currentPage: 1, //页码
      pageSize: 3, //每页显示的数量
      total: 100, //分页总数量
      searchForm: { title: "" }, //查询携带的参数
      dialogFormVisible: false, //使弹出不显示
      title: "关于添加", //title 默认为 攻略添加
      //添加或者更新数据时 携带的参数
      form: {},
      //弹出框的宽度
      formLabelWidth: "120px",
    };
  },
  //初始化数据
  created() {
    this.loadData();
  },
  methods: {
    loadData() {
      //不是直接使用axios 而是使用$http
      //baseURL="http://localhost:8080"
      var _this = this;
      this.$http
        .post("/tuser/queryPage", {
          pageNo: this.currentPage,
          pageSize: this.pageSize,
        })
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            //this不可以直接使用 代表的是axios对象
            _this.tableData = response.data.data.records;
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //用户性别 把状态数字,转换成字符
    getStatus(row, column, cellValue, index) {
      return cellValue == 1 ? "男" : "女";
    },
    getStatus1(row, column, cellValue, index) {
      return cellValue == 0 ? "有效" : "无效";
    },
    //实行每页有多少条的方法
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.loadData();
    },
    //实现跳转页的方法
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.loadData();
    },
    //刷新
    refresh() {
      //原始js重新请求
      location.reload();
    },
    //查询方法
    search() {
      this.loadData();
      //搜索时,使页码为1
      this.currentPage = 1;
    },
    //攻略更新的方法
    handleEdit(index, row) {
      console.log(index, row);
      //使弹出框显示
      this.dialogFormVisible = true;
      //给用户赋值
      this.title = "关于更新";
      //点击查询携带数据
      this.form = row;
      this.imageUrls = row.imageUrl;
    },
    //添加数据方法
    handleAdd() {
      //使弹出框显示
      this.dialogFormVisible = true;
      //给用户赋值
      this.title = "关于添加";
      //点击查询携带数据
      this.form = {};
      this.imageUrls = "";
    },
    //关闭方法
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //删除方法
    deleteDept(id) {
      this.$confirm("此操作将删除该关于, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var _this = this;
          this.$http
            .delete("/tuser/deleteById?id=" + id)
            .then(function (response) {
              console.log(response);
              if (response.data.code == 200) {
                _this.$message({
                  message: "操作成功",
                  type: "success",
                });
                _this.dialogFormVisible = false;
                _this.loadData();
              } else {
                _this.$message("操作失败");
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //实现用户添加或者更新
    addOrUpdate() {
      var uri = "/tuser/add";
      if (this.title == "关于更新") {
        uri = "/tuser/update";
      }
      var _this = this;
      this.$http
        .post(uri, this.form)
        .then(function (response) {
          console.log(response);
          if (response.data.code == 200) {
            _this.$message({
              message: "操作成功",
              type: "success",
            });
            _this.dialogFormVisible = false;
            _this.loadData();
          } else {
            _this.$message("操作失败");
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
